import { ElMessage, ElMessageBox } from 'element-plus'
export const resMessage = ({ res, btnText, success: cg, error: sb, danger, successText = '成功', errorText = '失败', handle } = {}) => {
  const { code, data, message } = res
  if (code !== 200) {
    ElMessage.error(message)
    sb && sb(data)
    return Promise.reject(data)
  }
  const { count, success, fail, success_message, fail_message } = data || {}
  if (!fail) {
    cg && cg(data)
    return
  }
  danger && danger()
  return ElMessageBox({
    title: '提示',
    customClass: 'message-box-520',
    showCancelButton: !handle,
    showConfirmButton: !!btnText && !handle,
    cancelButtonText: '关闭',
    confirmButtonText: btnText,
    closeOnClickModal: false,
    message: () => <div class='flex flex-col gap-2 w-full' style='width: 800px;max-height: calc(100vh - 160px);overflow-x: hidden;overflow-y: auto'>
      <div class="grid grid-cols-3 gap-x-3">
        <strong>全部: <span class='text-blue-600'>{count || 0}</span></strong>
        <strong>{successText}: <span class='text-green-600'>{success || 0}</span></strong>
        <strong>{errorText}: <span class='text-red-600'>{fail || 0}</span></strong>
      </div>
      {
        success_message ? <div class='mb-2'>
          <div class='text-green-600 flex items-center gap-1 mb-1 font-bold'>
            <i class="dk-iconfont icon-SuccessFilled" />{successText}：</div>
          {success_message.split('\n').filter(Boolean).map(d => <div>{d}</div>)}
        </div> : ''
      }
      {fail_message ? <div>
        <div class='text-red-600 flex items-center gap-1 mb-1 font-bold'>
          <i class="dk-iconfont icon-CircleCloseFilled" />{errorText}：</div>
        {fail_message.split('\n').filter(Boolean).map(d => <div>{d}</div>)}
      </div> : ''}
      {handle && handle()}
    </div>
  }).then(() => data)
}

export const resSkip = ({ res, success, submit, closeLoading, showMessage = true, props, params } = {}) => {
  const { code, data, message } = res
  const { is_skip } = data
  if (code === 200) {
    showMessage && ElMessage.success('保存成功')
    success && success()
    return
  }
  if (is_skip === 2) {
    ElMessageBox({
      title: '提示',
      type: 'error', 
      confirmButtonText: '是',
      cancelButtonText: '否',
      message: () => <div class='whitespace-pre-wrap'>{message}</div>,
      ...props
    }).then(() => {
      submit && submit(params)
    }).finally(() => {
      closeLoading && closeLoading()
    })
  } else {
    ElMessage.error(message)
    closeLoading && closeLoading()
  }
}